package com.study.bigdata.spark.streaming

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

object SparkStreaming07_Output {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkStreaming")
    val ssc =new StreamingContext(sparkConf,Seconds(3))
    ssc.checkpoint("cp")

    val lines = ssc.socketTextStream("localhost", 9999)
    val wordToOne = lines.map((_, 1))

    val wordToCount = wordToOne.reduceByKeyAndWindow(
      (x: Int, y: Int) => {x + y},
      (x: Int, y: Int) => {x - y},
      Seconds(9), Seconds(3)
    )
    //如果StreamingContext中没有设定输出操作，整个context就都不会启动
    //requirement failed: No output operations registered, so nothing to execute
//    wordToCount.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
